<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /*
                                                                                                        1.将一个JS数组通过for循环的方式, 生成一个反转过来的数组
                                                                                                        stu = ['tom', 'berry', 'ketty', 'rose', 'jake'];
                                                                                                        result = ['jake', 'rose', 'ketty', 'berry', 'tom'];
                                                                                                       */
        var stu = ['tom', 'berry', 'ketty', 'rose', 'jake'];
        var newStu = [];
        for (let i = stu.length - 1; i >= 0; i--) {
            newStu.push(stu[i]);
        }
        console.log(newStu);
        // reverse()  反序
        console.log(stu.reverse());


        /*  
         2. 分别写三个函数, 分别通过while, for, for (x in countries) 三种方法, 将数组连接成一个字符串, 注意: 不是使用js自带的方法
         countries = ["中国", "美国", "英国", "法国", "巴西", "墨西哥"];
         结果: resule = 中国, 美国, 英国, 法国, 巴西, 墨西哥;
        */
        let countries = ["中国", "美国", "英国", "法国", "巴西", "墨西哥"];
        let result = countries[0];
        //  for (let i = 1; i < countries.length; i++) {
        //     result += ","+countries[i];
        //  }
        /*
        var i = 1;
        while(i < countries.length){
            result += ","+countries[i];
            i++;
        }
        */
        for (const i in countries) {
            // continue 继续执行下一个循环
            if (i == 0) {
                continue;
            }
            result += "," + countries[i];
        }
        console.log(result);
        // toString() 把数组转换成字符串 用,(默认)隔开
        console.log(countries.toString());

        /*
        3.  写一个函数, 判断一个字符串是否在数组中, 在返回true, 不在返回false
        countries = ["中国", "美国", "英国", "法国", "巴西", "墨西哥"];
        中国, 结果true
        阿根廷, 结果false
     */
        countries = ["中国", "美国", "英国", "法国1", "巴西", "墨西哥"];
        console.log(findCountries("中国", countries));
        console.log(findCountries("阿根廷", countries));

        function findCountries(country, countries) {
            for (const key in countries) {
                if (countries[key] == country) {
                    return true;
                }
            }
            return false;
        }
        // includes()  是否包含
        console.log(countries.includes("法国1"))


        /*
           4. 假设某人有100000元钱, 每到一个学校, 都需要做些捐赠, 
           当他的现金 > 50000元时, 他每次捐献 5 %, 反之, 
           每次捐献2000, 直到剩下的钱不够2000元, 
           停止捐献, 请问 这个大慈善家 一共可以 做多少次捐献
        */
        let money = 100000;
        let num = 0;
        while (money > 2000) {
            if (money > 50000) {
                money -= money * 0.05;
            } else {
                money -= 2000;
            }
            num++;
        }
        console.log(num);

        /*
           5. 定义一个数组为每个的存储金额, 通过 循环判断, 得到所有 金额 >= 6000的人, 并将符合条件的人生成一个json格式数组
           deposit = [
               { "id": 1, "names": "小明", "money": 8000 },
               { "id": 2, "names": "小李", "money": 9000 },
               { "id": 3, "names": "小张", "money": 6000 },
               { "id": 4, "names": "小陈", "money": 10000 },
               { "id": 5, "names": "小王", "money": 7000 },
               { "id": 6, "names": "小毛", "money": 5000 },
               { "id": 7, "names": "小赵", "money": 5600 },
               { "id": 8, "names": "小彭", "money": 7888 },
           ];
        */
        let deposit = [{
            "id": 1,
            "names": "小明",
            "money": 8000
        }, {
            "id": 2,
            "names": "小李",
            "money": 9000
        }, {
            "id": 3,
            "names": "小张",
            "money": 6000
        }, {
            "id": 4,
            "names": "小陈",
            "money": 10000
        }, {
            "id": 5,
            "names": "小王",
            "money": 7000
        }, {
            "id": 6,
            "names": "小毛",
            "money": 5000
        }, {
            "id": 7,
            "names": "小赵",
            "money": 5600
        }, {
            "id": 8,
            "names": "小彭",
            "money": 7888
        }, ];
        let arr = [];
        deposit.forEach(res => {
            if (res.money >= 6000) {
                arr.push(res);
            }
        })
        console.log(arr);
        // filter()  过滤函数
        arr = deposit.filter(res => res.money >= 6000)
        console.log(arr);

        /*
        6.定义一个函数, 找到提供的句子中最长的单词。
        如: I am a good student  结果: student
    */
        let str6 = "I am a good student";

        function findWord(str) {
            let arr = str.split(" ");
            let maxWord = arr[0];
            arr.forEach(res => {
                if (res.length > maxWord.length) {
                    maxWord = res;
                }
            })
            return maxWord;
        }
        console.log(findWord(str6))

        /* 
    7.定义一个函数, 确保字符串的每个单词首字母都大写，其余部分小写。
   I am a good student  结果: I Am A Good Student;
   */
        console.log(firstUp(str6));

        function firstUp(str) {
            let newStr = "";
            let arr = str.split(" ");
            arr.forEach(res => {
                //console.log( res.slice(0,1).toUpperCase() );
                //console.log( res.slice(1).toLowerCase() );
                newStr += res.slice(0, 1).toUpperCase();
                newStr += res.slice(1).toLowerCase();
                newStr += " ";
            })
            return newStr;
        }

        /*
            8. 给出一个数组, 每次刷新浏览器, 生成随机的4个值
            Arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, “A”,”B”,”c”,”D”,”E”,”F”,”G”,”H”];
        */
        let arr8 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'c', 'D', 'E', 'F', 'G', 'H'];
        // parseInt() 强制转换成整数
        // Math.random() 生成0~1之间的随机数
        console.log(parseInt(Math.random() * arr8.length));
        let str8 = "";
        for (let i = 0; i < 4; i++) {
            let index = parseInt(Math.random() * arr8.length); //强制转换成整数
            str8 += arr8[index];
        }
        console.log(str8);
    </script>


</body>

</html>